function fetchDataWithGet(url) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('GET', url, true);
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                try {
                    let response = xhr.responseText;
                    if (xhr.getResponseHeader('Content-Type').includes('application/json')) {
                        response = JSON.parse(response);
                    }
                    resolve(response);
                } catch (e) {
                    reject(new Error('Failed to parse the GET response as JSON.'));
                }
            } else {
                reject(new Error(`GET request failed with status ${xhr.status}`));
            }
        };
        xhr.onerror = function () {
            reject(new Error('Network error occurred during GET request.'));
        };
        xhr.send();
    });
}

function fetchDataWithPost(url, data) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open('POST', url, true);
        xhr.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
        xhr.onload = function () {
            if (xhr.status >= 200 && xhr.status < 300) {
                try {
                    let response = xhr.responseText;
                    if (xhr.getResponseHeader('Content-Type').includes('application/json')) {
                        response = JSON.parse(response);
                    }
                    resolve(response);
                } catch (e) {
                    reject(new Error('Failed to parse the POST response as JSON.'));
                }
            } else {
                reject(new Error(`POST request failed with status ${xhr.status}`));
            }
        };
        xhr.onerror = function () {
            reject(new Error('Network error occurred during POST request.'));
        };
        xhr.send(JSON.stringify(data));
    });
}
